﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Erebos.GameScreenFiles.WorldObjects
{
    class FireworkObject : Object
    {
        private Vector3 _position;

        private Vector2 _velocity;

        private int _minHeight;
        private int _time;

        private List<Projectile> _projectiles = new List<Projectile>();

        private TimeSpan _timeToNextProjectile = TimeSpan.Zero;

        private ParticleSystem _fireworkParticles;
        private ParticleSystem _explosionSmokeParticles;
        private ParticleSystem _projectileTrailParticles;

        public FireworkObject(Vector3 position, Vector2 velocity, int minHeight, int time)
        {
            _position = position;
            _velocity = velocity;
            _minHeight = minHeight;
            _time = time;

            _fireworkParticles = new FireworkParticleSystem(MainGame.Game, MainGame.GetContentManager());
            _explosionSmokeParticles = new ExplosionSmokeParticleSystem(MainGame.Game, MainGame.GetContentManager());
            _projectileTrailParticles = new ProjectileTrailParticleSystem(MainGame.Game, MainGame.GetContentManager());

            MainGame.Game.Components.Add(_fireworkParticles);
            MainGame.Game.Components.Add(_explosionSmokeParticles);
            MainGame.Game.Components.Add(_projectileTrailParticles);
        }

        public override void Update(GameTime gameTime)
        {
            _fireworkParticles.DrawOrder = 2024 * 2024 - (int)Vector2.Distance(new Vector2(Camera.CameraPosition.X, -Camera.CameraPosition.Z), new Vector2(_position.X, _position.Z));
            _explosionSmokeParticles.DrawOrder = 2024 * 2024 - (int)Vector2.Distance(new Vector2(Camera.CameraPosition.X, -Camera.CameraPosition.Z), new Vector2(_position.X, _position.Z));
            _projectileTrailParticles.DrawOrder = 2024 * 2024 - (int)Vector2.Distance(new Vector2(Camera.CameraPosition.X, -Camera.CameraPosition.Z), new Vector2(_position.X, _position.Z));

            _timeToNextProjectile -= gameTime.ElapsedGameTime;

            if (_timeToNextProjectile <= TimeSpan.Zero)
            {
                Projectile p = new Projectile(_fireworkParticles,
                                              _explosionSmokeParticles,
                                              _projectileTrailParticles,
                                              new Vector3(_position.X, _position.Y, -_position.Z));
                _fireworkParticles.Initialize();
                p.VerticalVelocityRange = _velocity.Y;
                p.SidewaysVelocityRange = _velocity.X;
                p.MinVerticalVelocityRange = _minHeight;
                _projectiles.Add(p);

                _timeToNextProjectile += TimeSpan.FromSeconds(_time);
            }

            int i = 0;

            while (i < _projectiles.Count)
            {
                if (!_projectiles[i].Update(gameTime))
                {
                    _projectiles.RemoveAt(i);
                }
                else
                {
                    i++;
                }
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            _fireworkParticles.SetCamera(Camera.ViewMatrix, Camera.ProjectionMatrix);
            _explosionSmokeParticles.SetCamera(Camera.ViewMatrix, Camera.ProjectionMatrix);
            _projectileTrailParticles.SetCamera(Camera.ViewMatrix, Camera.ProjectionMatrix);

            base.Draw(gameTime);
        }
    }
}
